
package org.tinygroup.sdpm.zhang.dao.impl;

import static org.tinygroup.sdpm.zhang.dao.inter.constant.UserTable.USER_TABLE;
import static org.tinygroup.tinysqldsl.Delete.delete;
import static org.tinygroup.tinysqldsl.Insert.insertInto;
import static org.tinygroup.tinysqldsl.Select.selectFrom;
import static org.tinygroup.tinysqldsl.Update.update;
import static org.tinygroup.tinysqldsl.base.StatementSqlBuilder.and;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import org.tinygroup.commons.tools.CollectionUtil;
import org.tinygroup.jdbctemplatedslsession.callback.DeleteGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.InsertGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.NoParamDeleteGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.NoParamInsertGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.NoParamUpdateGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.SelectGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.callback.UpdateGenerateCallback;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.jdbctemplatedslsession.daosupport.TinyDslDaoSupport;
import org.tinygroup.jdbctemplatedslsession.util.TinyDSLUtil;
import org.tinygroup.sdpm.zhang.dao.inter.UserDao;
import org.tinygroup.sdpm.zhang.dao.inter.pojo.User;
import org.tinygroup.tinysqldsl.Delete;
import org.tinygroup.tinysqldsl.Insert;
import org.tinygroup.tinysqldsl.Pager;
import org.tinygroup.tinysqldsl.Select;
import org.tinygroup.tinysqldsl.Update;
import org.tinygroup.tinysqldsl.expression.JdbcNamedParameter;

/**
 * <!-- begin-user-doc --> 如果不希望某方法或者变量被覆盖，可以删除方法或者变量的注释@modifiable <!--
 * end-user-doc -->
 */
@Repository
public class UserDaoImpl extends TinyDslDaoSupport implements UserDao {

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public User add(User user) {
		return getDslTemplate().insertAndReturnKey(user, new InsertGenerateCallback<User>() {
			public Insert generate(User t) {
				Insert insert = insertInto(USER_TABLE).values(USER_TABLE.USER_ID.value(t.getUserId()),
						USER_TABLE.USER_NAME.value(t.getUserName()), USER_TABLE.USER_AGE.value(t.getUserAge()),
						USER_TABLE.JOIN_TIME.value(t.getJoinTime())

				);
				return insert;
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int edit(User user) {
		if (user == null || StringUtils.isBlank(user.getUserId())) {
			return 0;
		}
		return getDslTemplate().update(user, new UpdateGenerateCallback<User>() {
			public Update generate(User t) {
				Update update = update(USER_TABLE)
						.set(USER_TABLE.USER_NAME.value(t.getUserName()), USER_TABLE.USER_AGE.value(t.getUserAge()),
								USER_TABLE.JOIN_TIME.value(t.getJoinTime()))
						.where(USER_TABLE.USER_ID.eq(t.getUserId()));
				return update;
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int deleteByKey(String pk) {

		if (StringUtils.isBlank(pk)) {
			return 0;
		}
		return getDslTemplate().deleteByKey(pk, new DeleteGenerateCallback<Serializable>() {
			public Delete generate(Serializable pk) {
				return delete(USER_TABLE).where(USER_TABLE.USER_ID.eq(pk));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int deleteByKeys(String... pks) {
		if (pks == null || pks.length == 0) {
			return 0;
		}
		for (String pk : pks) {
			if (StringUtils.isBlank(pk)) {
				return 0;
			}
		}
		return getDslTemplate().deleteByKeys(new DeleteGenerateCallback<Serializable[]>() {
			public Delete generate(Serializable[] t) {
				return delete(USER_TABLE).where(USER_TABLE.USER_ID.in(t));
			}
		}, pks);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public User getByKey(String pk) {
		return getDslTemplate().getByKey(pk, User.class, new SelectGenerateCallback<Serializable>() {

			@SuppressWarnings("rawtypes")
			public Select generate(Serializable t) {
				return selectFrom(USER_TABLE).where(USER_TABLE.USER_ID.eq(t));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public List<User> query(User user, final OrderBy... orderArgs) {
		if (user == null) {
			user = new User();
		}
		return getDslTemplate().query(user, new SelectGenerateCallback<User>() {
			@SuppressWarnings("rawtypes")
			public Select generate(User t) {
				Select select = selectFrom(USER_TABLE)
						.where(and(USER_TABLE.USER_ID.eq(t.getUserId()), USER_TABLE.USER_NAME.eq(t.getUserName()),
								USER_TABLE.USER_AGE.eq(t.getUserAge()), USER_TABLE.JOIN_TIME.eq(t.getJoinTime())

				));
				return TinyDSLUtil.addOrderByElements(select, orderArgs);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public Pager<User> queryPager(int start, int limit, User user, final OrderBy... orderArgs) {
		if (user == null) {
			user = new User();
		}
		return getDslTemplate().queryPager(start, limit, user, false, new SelectGenerateCallback<User>() {
			public Select generate(User t) {
				Select select = Select.selectFrom(USER_TABLE)
						.where(and(USER_TABLE.USER_ID.eq(t.getUserId()), USER_TABLE.USER_NAME.eq(t.getUserName()),
								USER_TABLE.USER_AGE.eq(t.getUserAge()), USER_TABLE.JOIN_TIME.eq(t.getJoinTime())

				));
				return TinyDSLUtil.addOrderByElements(select, orderArgs);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchInsert(boolean autoGeneratedKeys, List<User> user) {
		if (CollectionUtil.isEmpty(user)) {
			return new int[0];
		}
		return getDslTemplate().batchInsert(autoGeneratedKeys, user, new InsertGenerateCallback<User>() {

			public Insert generate(User t) {
				return insertInto(USER_TABLE).values(USER_TABLE.USER_ID.value(t.getUserId()),
						USER_TABLE.USER_NAME.value(t.getUserName()), USER_TABLE.USER_AGE.value(t.getUserAge()),
						USER_TABLE.JOIN_TIME.value(t.getJoinTime())

				);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchInsert(List<User> users) {
		return batchInsert(false, users);
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchUpdate(List<User> users) {
		if (CollectionUtil.isEmpty(users)) {
			return new int[0];
		}
		for (User user : users) {
			if (StringUtils.isBlank(user.getUserId())) {
				return new int[0];
			}
		}
		return getDslTemplate().batchUpdate(users, new UpdateGenerateCallback<User>() {
			public Update generate(User t) {
				return update(USER_TABLE).set(USER_TABLE.USER_NAME.value(t.getUserName()),
						USER_TABLE.USER_AGE.value(t.getUserAge()), USER_TABLE.JOIN_TIME.value(t.getJoinTime())

				).where(USER_TABLE.USER_ID.eq(t.getUserId()));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] batchDelete(List<User> users) {
		if (CollectionUtil.isEmpty(users)) {
			return new int[0];
		}
		for (User user : users) {
			if (

			StringUtils.isBlank(user.getUserId()) &&

					StringUtils.isBlank(user.getUserName()) &&

					StringUtils.isBlank(String.valueOf(user.getUserAge())) &&

					StringUtils.isBlank(String.valueOf(user.getJoinTime()))

			) {
				return new int[0];
			}
		}
		return getDslTemplate().batchDelete(users, new DeleteGenerateCallback<User>() {
			public Delete generate(User t) {
				return delete(USER_TABLE)
						.where(and(USER_TABLE.USER_ID.eq(t.getUserId()), USER_TABLE.USER_NAME.eq(t.getUserName()),
								USER_TABLE.USER_AGE.eq(t.getUserAge()), USER_TABLE.JOIN_TIME.eq(t.getJoinTime())

				));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchInsert(boolean autoGeneratedKeys, List<User> user) {
		if (CollectionUtil.isEmpty(user)) {
			return new int[0];
		}
		return getDslTemplate().batchInsert(autoGeneratedKeys, user, new NoParamInsertGenerateCallback() {

			public Insert generate() {
				return insertInto(USER_TABLE).values(USER_TABLE.USER_NAME.value(new JdbcNamedParameter("userName")),
						USER_TABLE.USER_AGE.value(new JdbcNamedParameter("userAge")),
						USER_TABLE.JOIN_TIME.value(new JdbcNamedParameter("joinTime"))

				);
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchUpdate(List<User> users) {
		if (CollectionUtil.isEmpty(users)) {
			return new int[0];
		}

		for (User user : users) {
			if (StringUtils.isBlank(user.getUserId())) {
				return new int[0];
			}
		}
		return getDslTemplate().batchUpdate(users, new NoParamUpdateGenerateCallback() {
			public Update generate() {
				return update(USER_TABLE).set(USER_TABLE.USER_NAME.value(new JdbcNamedParameter("userName")),
						USER_TABLE.USER_AGE.value(new JdbcNamedParameter("userAge")),
						USER_TABLE.JOIN_TIME.value(new JdbcNamedParameter("joinTime"))

				).where(USER_TABLE.USER_ID.eq(new JdbcNamedParameter("userId")));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchDelete(List<User> users) {
		if (CollectionUtil.isEmpty(users)) {
			return new int[0];
		}

		for (User user : users) {
			if (

			StringUtils.isBlank(user.getUserId()) &&

					StringUtils.isBlank(user.getUserName()) &&

					StringUtils.isBlank(String.valueOf(user.getUserAge())) &&

					StringUtils.isBlank(String.valueOf(user.getJoinTime()))

			) {
				return new int[0];
			}
		}

		return getDslTemplate().batchDelete(users, new NoParamDeleteGenerateCallback() {
			public Delete generate() {
				return delete(USER_TABLE).where(and(USER_TABLE.USER_NAME.eq(new JdbcNamedParameter("userName")),
						USER_TABLE.USER_AGE.eq(new JdbcNamedParameter("userAge")),
						USER_TABLE.JOIN_TIME.eq(new JdbcNamedParameter("joinTime"))

				));
			}
		});
	}

	/**
	 * <!-- begin-user-doc --> <!-- end-user-doc -->
	 * 
	 * @modifiable
	 */
	public int[] preparedBatchInsert(List<User> user) {
		return preparedBatchInsert(false, user);
	}

}
